package org.poker.prophecy.grafix.svgcards.test;

import java.awt.Dimension;
import java.awt.Graphics;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URI;

import javax.swing.JPanel;

import com.kitfox.svg.SVGCache;
import com.kitfox.svg.app.beans.SVGIcon;

public class IconPanel extends JPanel {

	public static final long serialVersionUID = 0;

	final SVGIcon icon;

	public IconPanel() {

		StringReader reader = new StringReader(makeDynamicSVG());
		URI uri = SVGCache.getSVGUniverse().loadSVG(reader, "myImage");
		// SVGDiagram diagram = SVGCache.getSVGUniverse().getDiagram(uri);
		icon = new SVGIcon();
		icon.setSvgURI(uri);

		setPreferredSize(new Dimension(400, 400));

	}

	public void paintComponent(Graphics g) {
		final int width = getWidth();
		final int height = getHeight();

		g.setColor(getBackground());
		g.fillRect(0, 0, width, height);

		icon.paintIcon(this, g, 0, 0);
	}

	private String makeDynamicSVG() {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);

		pw
				.println("<svg width=\"400\" height=\"400\" style=\"fill:none;stroke-width:4\">");
		pw
				.println("    <circle cx=\"200\" cy=\"200\" r=\"200\" style=\"stroke:blue\"/>");
		pw
				.println("    <circle cx=\"140\" cy=\"140\" r=\"40\" style=\"stroke:red\"/>");
		pw
				.println("    <circle cx=\"260\" cy=\"140\" r=\"40\" style=\"stroke:red\"/>");
		pw
				.println("    <polyline points=\"100 300 150 340 250 240 300 300\" style=\"stroke:red\"/>");
		pw.println("</svg>");

		pw.close();
		return sw.toString();
	}

}
